define(function() {
    return ['$scope','$location', '$interval','$timeout','$routeParams','bizProxy', function($scope,$location,$interval,$timeout,$routeParams,bizProxy) {
        $scope.$emit("top-show",'true');//顶部显示
        $scope.$emit('menuTypeLight','mine'); 	//控制底部menu高亮
        $scope.$emit('bub-menu','true'); 	//隐藏底部menu
        $scope.$emit('flag-tag','71');//显示底部文字
        
        var sysNotRead = new Array(),
        	busNotRead = new Array(),
            messageIdList = new Array();

        queryMessage();
        
        $scope.goNext=function (name) {
            $scope.$emit("goNext","/messageList/"+name);
        }
        $scope.time_system='';
        $scope.time_notify='';
        //分类处理数据
        function checkData(list){
    		var contactArryData;
    		//保存message信息在localstorage中
        	var tempMessage = localStorage.getItem("firflyMessageInfo");
        	if(!tempMessage && list.length == 0){
        		return;
        	}
        	if(!tempMessage){
        		localStorage.setItem("firflyMessageInfo",JSON.stringify(list));
        		contactArryData = list;
        	}else{
                console.log("tempMessage=",JSON.parse(tempMessage))
        		var tempJsonData = JSON.parse(tempMessage);
        		contactArryData = list.concat(tempJsonData);
        		localStorage.setItem("firflyMessageInfo",JSON.stringify(contactArryData));
        	}
        	var time_system=0;
        	var time_notify=0;
        	contactArryData.forEach(function(v){
        		if(v.messageKind == 'system'){
                    if(Number(v.messageTime)>time_system){
                        time_system=Number(v.messageTime)
                    }
                    if(v.readStatus == '1'){
                        sysNotRead.push(v);
                    }
        		}
        		if(v.messageKind == 'notify'){
                    if(Number(v.messageTime)>time_notify){
                        time_notify=Number(v.messageTime)
                    }
                    if(v.readStatus == '1'){
                        busNotRead.push(v);
                    }
        		}
                if(time_system){
                    $scope.time_system=String(time_system).substring(0,4)+"-"+String(time_system).substring(4,6)+"-"+String(time_system).substring(6,8)
                }
                if(time_notify){
                    $scope.time_notify=String(time_notify).substring(0,4)+"-"+String(time_notify).substring(4,6)+"-"+String(time_notify).substring(6,8)
                }
        		messageIdList.push(v.messageId);
        	});

        	$timeout(function(){
        		$scope.sysNotReadNum = sysNotRead.length;
            	$scope.busNotReadNum = busNotRead.length;
        	},0);

        	//保存最大messageId
        	if(messageIdList.length > 0){
        		var lastMessageId = Math.max.apply(null, messageIdList);
        		localStorage.setItem("firflyLastMessageId",lastMessageId);
        	}
        }
        
        
        //消息查询
        function queryMessage(){
        	var headerInfo = {},bizInfo = {};
        	headerInfo.pageNum = '';
        	headerInfo.pageSize = '';
        	headerInfo.sessionId = transData.sessionId;
        	
        	var tempLastMessageId = localStorage.getItem("firflyLastMessageId");
        	if(tempLastMessageId){
        		bizInfo.lastMessageId = tempLastMessageId;
        	}else{
        		bizInfo.lastMessageId = "";
        	}
        	console.log(bizInfo)
    		$scope.$emit("loadingChangeNav",true);
    		bizProxy.queryProxyBizData("queryMessage",headerInfo,bizInfo,
    				function(tempData){//SUCCESS
                        console.log("消息查询=",tempData)
		    			if(tempData.body != "" && tempData.body != null && tempData.body != "null" && tempData.body != "[]"){
							var tempMsgList = JSON.parse(tempData.body);
							$timeout(function(){
								$scope.$emit("loadingChangeNav",false);
								checkData(tempMsgList);
							},0);
						}else{
							$timeout(function(){
								$scope.$emit("loadingChangeNav",false);
								checkData(new Array());
							},0);
						}
		    		},function(data){//FAIL
					console.log(data)
		    			$scope.$emit("loadingChangeNav",false);
		    			$timeout(function(){
		    				checkData(new Array());
		    				$scope.$emit("promptAlertMsg",data.errorMsg);
							$scope.$emit("showCollect",true);
							$timeout(function(){
							},transData.promptTime);
						},0);
		    		});
        }

        $scope.$apply();
    }];
});